package designpatterns.singleton;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description: 懒汉单例模式
 * @author: LiDong
 * @time: 2020/7/21 20:24
 **/
public class LazySingleton {

    private static final Logger logger = LoggerFactory.getLogger(LazySingleton.class);

    /**
     * 保证lazySingleton在线程中同步
     */
    private static volatile LazySingleton lazySingleton;


    /**
     * 保证类不在别的地方被实例化
     */
    private LazySingleton() {
    }

    /**
     * synchronize保证线程安全
     */
    public static synchronized LazySingleton getInstance() {
        if (null == lazySingleton) {
            logger.info("懒汉式单例创建对象！");
            lazySingleton = new LazySingleton();
        }
        return lazySingleton;
    }

}
